import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def send_mail(report_file):
    #report_file = "../test/report.html"
    # 读取测试报告内容
    with open(report_file, 'r', encoding="utf-8") as f:
        content = f.read()

    msg = MIMEMultipart('mixed')
    # 添加邮件内容
    msg_html = MIMEText(content, 'html', 'utf-8')
    msg.attach(msg_html)

    # 添加附件
    msg_attachment = MIMEText(content, 'html', 'utf-8')
    # test.html为附件的名字，可以自己命名
    msg_attachment["Content-Disposition"] = 'attachment; filename="test.html"'
    msg.attach(msg_attachment)

    # 邮件主题描述
    mail_subjet = "这是个自动化测试结果邮件"
    # 发件人
    mail_user = "15818549324@163.com"
    # 邮件接受方邮箱地址，注意需要[]包裹，这意味着你可以写多个邮件地址群发
    receivers = ['15818549324@163.com']
    # 第三方smtp服务器
    mail_host = "smtp.163.com"
    # 密码，这里用的授权码
    mail_pwd = "RSHYMJTMFBMWCZEA"

    msg['Subject'] = mail_subjet
    msg['From'] = mail_user
    msg['To'] = ";".join(receivers)
    try:
        # 连接邮件服务器，25是端口号，如果换其他邮箱需要修改
        s = smtplib.SMTP(mail_host, 25)
        # 登陆
        s.login(mail_user, mail_pwd)
        # 发送邮件
        s.sendmail(mail_user, receivers, msg.as_string())
        # 退出
        s.quit()
    except Exception as e:
        print(e)
